package com.linrung.sf.scheduler.zwwx.jms;

import java.util.List;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.linrung.sf.scheduler.zwwx.entity.JMSSendParamVO;

/**
 * @author 袁彬
 * 政务微信消息推送生产者
 *
 */
@Service("zwwxMsgProductorService")
public class ZWWXMsgProductorService {

	@Autowired
	@Qualifier("jmsQueueTemplate")
	private JmsTemplate jmsTemplate;
	
	public static String QUEUE_NAME = "send.zwwx.msg.queue";
	public static String MSG_CONTENT = "zwsxMsgcontent";
	public static String PF_MESSAGE_RECEIVE_ID = "pfMsgReceiveId";
	public static String SOURCECODE = "sourceCode";
	public static String MSG_TYPE = "msgType";
	
	//这个等调用的地方写完数据库后一次发送
	public void send(List<JMSSendParamVO> sendList) {
		for(JMSSendParamVO param : sendList) {
			String pfMsgReceiveId = param.getPfMsgReceiveId();
			String sourceCode = param.getSourceCode();
			String zwsxMsgcontent = param.getZwsxMsgcontent();
			String msgType = param.getMsgType();

			jmsTemplate.send(QUEUE_NAME, new MessageCreator() {
				@Override
				public javax.jms.Message createMessage(Session session) throws JMSException {
					//设置队列中需要的参数
					MapMessage mapMsg = session.createMapMessage();
					mapMsg.setStringProperty(MSG_CONTENT, zwsxMsgcontent);
					mapMsg.setStringProperty(PF_MESSAGE_RECEIVE_ID, pfMsgReceiveId);
					mapMsg.setStringProperty(SOURCECODE, sourceCode);
					mapMsg.setStringProperty(MSG_TYPE, msgType);
					return mapMsg;
				}
			});
		}
		
	}
}
